import matplotlib.pyplot as plt
import numpy as np
# 图表动态显示库
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

# 自动刷新的函数, i在每一次执行更新的时候会+1
def animate(i):
    # line.set_xdata 设置x的坐标值
    # line.set_ydata 设置y的坐标值
    # line.set_data(x,y) 同时设置x，y的坐标
    line.set_ydata(np.sin(x + i / 50))  # update the data.
    return line,

# 以间隔20ms的速度刷新整个图表
ani = animation.FuncAnimation(
    fig, animate, interval=20, blit=True, save_count=50)


plt.show()